package xyz.zdsoft.ask_for_leave_manage_module.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import xyz.zdsoft.ask_for_leave_manage_module.vo.StudentLeaveForTeacherVo;

@Mapper
public interface TeacherLeavesMapper {

    /**
     * 查询老师能够看到的学生请假记录
     * @param page mybatis-plus分页
     * @param wrapper mybatis-plus查询条件
     * @Param teaId 教师id
     * @return 学生请假记录
     */
    @Select({"<script>","SELECT DISTINCT\n" +
            "            submit_time, colleges_name, major_name,\n" +
            "            grade_name, class_name, user_no, stu_name,\n" +
            "            theme, leave_type, reason, start_leave_time,\n" +
            "            end_leave_time, `status`, leave_id, \n" +
            "            stu_name IN (SELECT stu_name FROM leave_info_view liv WHERE liv.tea_id = #{teaId}) tag\n" +
            "FROM leave_info_view liv" +
            "<if test=\"ew.sqlSegment != null and ew.sqlSegment != ''\">\n" +
            "            where ${ew.sqlSegment}\n" +
            "        </if>",
            "</script>"})
    IPage<StudentLeaveForTeacherVo> queryStuLeaveForTea(IPage<StudentLeaveForTeacherVo> page,
                                                        @Param("ew") Wrapper<StudentLeaveForTeacherVo> wrapper,
                                                        @Param("teaId") Integer teaId);

    /**
     * 查询请假对应的班主任
     * @param leaId 请假id
     * @return 班主任id
     */
    @Select("SELECT tea.id FROM `leave` l\n" +
            "left join class_student_info csi\n" +
            "on l.student_id = csi.student_id\n" +
            "left join class_info ci\n" +
            "on csi.class_info_id = ci.id\n" +
            "left join sys_user tea\n" +
            "on ci.teacher_id = tea.id\n" +
            "WHERE l.id = #{leaId}")
    Integer queryLeaveOfTeacher(@Param("leaId") Integer leaId);
}
